<?php
class AdminKqMienbac extends CActiveRecord{

    public $arrSK = array(01,10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98); 

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'ketqua_mienbac';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getCountByDate($date){

    }
    public function getIdByDate($date){
        $connect = Yii::app()->db;
        $sql = "SELECT id FROM ketqua_mienbac WHERE ngay_quay='".$date."'";
        $command = $connect->createCommand($sql);  
        $row = $command->queryRow();   
        return $row;
    }
    public function getRowByDate($date=""){

        $day_now = date("Y-m-d",time()); 

        $row = array();

        if($date==""){
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,giai_nhi_1,giai_nhi_2,giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,
            giai_ba_5,giai_ba_6,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,
            giai_nam_6,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4
            FROM ketqua_mienbac  WHERE ORDER BY ngay_quay DESC LIMIT 0,1";
        }else{
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,giai_nhi_1,giai_nhi_2,giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,
            giai_ba_5,giai_ba_6,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,
            giai_nam_6,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4
            FROM ketqua_mienbac WHERE ngay_quay='".$date."'";
        }

        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);

        $row = $command->queryRow();

        return $row;
    }
    
    public function getRowById($id=""){

        $day_now = date("Y-m-d",time()); 

        $row = array();

        if($id==""){
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,giai_dacbiet,giai_nhat,giai_nhi_1,giai_nhi_2,giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,
            giai_ba_5,giai_ba_6,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,
            giai_nam_6,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4
            FROM ketqua_mienbac ORDER BY ngay_quay DESC LIMIT 0,1";
        }else{
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,giai_dacbiet,giai_nhat,giai_nhi_1,giai_nhi_2,giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,
            giai_ba_5,giai_ba_6,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,
            giai_nam_6,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4
            FROM ketqua_mienbac WHERE id='".$id."'";
        }

        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);

        $row = $command->queryRow();

        return $row;
    }
    public function getCount($begin="",$end=""){
        $row = array();
        if($begin=="" && $end==""){
            $sql = "SELECT count(*) as count
            FROM ketqua_mienbac";
        } else{
            if($begin<=$end)
            {
                $sql = "SELECT count(*) as count 
                FROM ketqua_mienbac WHERE ngay_quay >= '".$begin."' AND ngay_quay <= '".$end."'";
            } else{
                $sql = " id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user
                FROM ketqua_mienbac WHERE ngay_quay >= '".$end."' AND ngay_quay <= '".$begin."'";
            }
        }
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }

    public function getRowInDate($first,$row_per_page,$begin="",$end=""){
        $day_now = date("Y-m-d",time()); 
        $rows = array();

        if($begin=="" && $end==""){
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
            FROM ketqua_mienbac ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
        } else{
            if($begin<=$end)
            {
                $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
                FROM ketqua_mienbac WHERE ngay_quay >= '".$begin."' AND ngay_quay <= '".$end."' ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            } else{
                $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
                FROM ketqua_mienbac WHERE ngay_quay >= '".$end."' AND ngay_quay <= '".$begin."' ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            }
        }

        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();

        return $rows;
    }  



    /*public function getRowById($id){

        $row = array();
        $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,giai_nhi_1,giai_nhi_2,giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,
        giai_ba_5,giai_ba_6,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,
        giai_nam_6,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4
        FROM ketqua_mienbac WHERE id=".$id." ORDER BY ngay_quay DESC";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }*/ 

    public function insertGiaiBay($giai_bay_1,$giai_bay_2,$giai_bay_3,$giai_bay_4,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_bay_1,giai_bay_2,giai_bay_3,giai_bay_4,ngay_quay,create_user,create_date) 
        VALUES (:giai_bay_1, :giai_bay_2, :giai_bay_3, :giai_bay_4, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_bay_1", $giai_bay_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_bay_2", $giai_bay_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_bay_3", $giai_bay_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_bay_4", $giai_bay_4, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiSau($giai_sau_1,$giai_sau_2,$giai_sau_3,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_sau_1,giai_sau_2,giai_sau_3,ngay_quay,create_user,create_date) 
        VALUES (:giai_sau_1, :giai_sau_2, :giai_sau_3, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_sau_1", $giai_bay_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_2", $giai_sau_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_sau_3", $giai_sau_3, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiNam($giai_nam_1,$giai_nam_2,$giai_nam_3,$giai_nam_4,$giai_nam_5,$giai_nam_6,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_nam_1,giai_nam_2,giai_nam_3,giai_nam_4,giai_nam_5,giai_nam_6,ngay_quay,create_user,create_date) 
        VALUES (:giai_nam_1, :giai_nam_2, :giai_nam_3, :giai_nam_4, :giai_nam_5, :giai_nam_6, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nam_1", $giai_nam_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_2", $giai_nam_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_nam_3", $giai_nam_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_4", $giai_nam_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_5", $giai_nam_5, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_6", $giai_nam_6, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiTu($giai_tu_1,$giai_tu_2,$giai_tu_3,$giai_tu_4,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,ngay_quay,create_user,create_date) 
        VALUES (:giai_tu_1, :giai_tu_2, :giai_tu_3, :giai_tu_4, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_tu_1", $giai_tu_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_2", $giai_tu_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_tu_3", $giai_tu_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_4", $giai_tu_4, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiBa($giai_ba_1,$giai_ba_2,$giai_ba_3,$giai_ba_4,$giai_ba_5,$giai_ba_6,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_ba_1,giai_ba_2,giai_ba_3,giai_ba_4,giai_ba_5,giai_ba_6,ngay_quay,create_user,create_date) 
        VALUES (:giai_ba_1, :giai_ba_2, :giai_ba_3, :giai_ba_4, :giai_ba_5, :giai_ba_6, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_ba_1", $giai_ba_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_2", $giai_ba_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_ba_3", $giai_ba_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_4", $giai_ba_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_5", $giai_ba_5, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_6", $giai_ba_6, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiNhi($giai_nhi_1,$giai_nhi_2,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_nhi_1,giai_nhi_2,ngay_quay,create_user,create_date) 
        VALUES (:giai_nhi_1, :giai_nhi_2, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhi_1", $giai_nhi_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nhi_2", $giai_nhi_2, PDO::PARAM_STR );  
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertGiaiNhat($giai_nhat,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_nhat,ngay_quay,create_user,create_date) 
        VALUES (:giai_nhat,:ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhat", $giai_nhat, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    public function insertDacBiet($giai_dacbiet,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_mienbac(giai_dacbiet,ngay_quay,create_user,create_date) 
        VALUES (:giai_dacbiet,:ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_dacbiet", $giai_dacbiet, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }

    public function updateGiaiBay($id,$giai_bay_1,$giai_bay_2,$giai_bay_3,$giai_bay_4,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;

        $sql = "UPDATE ketqua_mienbac SET giai_bay_1 = :giai_bay_1, giai_bay_2 = :giai_bay_2,
        giai_bay_3 = :giai_bay_3, giai_bay_4 = :giai_bay_4, modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_bay_1", $giai_bay_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_bay_2", $giai_bay_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_bay_3", $giai_bay_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_bay_4", $giai_bay_4, PDO::PARAM_STR );        
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }

    public function updateGiaiSau($id,$giai_sau_1,$giai_sau_2,$giai_sau_3,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_sau_1 = :giai_sau_1, giai_sau_2 = :giai_sau_2,
        giai_sau_3 = :giai_sau_3, modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_sau_1", $giai_sau_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_2", $giai_sau_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_sau_3", $giai_sau_3, PDO::PARAM_STR );       
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateGiaiNam($id,$giai_nam_1,$giai_nam_2,$giai_nam_3,$giai_nam_4,$giai_nam_5,$giai_nam_6,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_nam_1 = :giai_nam_1, giai_nam_2 = :giai_nam_2,
        giai_nam_3 = :giai_nam_3, giai_nam_4 = :giai_nam_4, giai_nam_5 = :giai_nam_5, 
        giai_nam_6 = :giai_nam_6, modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nam_1", $giai_nam_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_2", $giai_nam_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_nam_3", $giai_nam_3, PDO::PARAM_STR );    
        $command->bindValue ( ":giai_nam_4", $giai_nam_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nam_5", $giai_nam_5, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_nam_6", $giai_nam_6, PDO::PARAM_STR );   
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateGiaiTu($id,$giai_tu_1,$giai_tu_2,$giai_tu_3,$giai_tu_4,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_tu_1 = :giai_tu_1, giai_tu_2 = :giai_tu_2,
        giai_tu_3 = :giai_tu_3, giai_tu_4 = :giai_tu_4, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_tu_1", $giai_tu_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_2", $giai_tu_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_tu_3", $giai_tu_3, PDO::PARAM_STR );    
        $command->bindValue ( ":giai_tu_4", $giai_tu_4, PDO::PARAM_STR ); 
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateGiaiBa($id,$giai_ba_1,$giai_ba_2,$giai_ba_3,$giai_ba_4,$giai_ba_5,$giai_ba_6,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_ba_1 = :giai_ba_1, giai_ba_2 = :giai_ba_2,
        giai_ba_3 = :giai_ba_3, giai_ba_4 = :giai_ba_4, giai_ba_5 = :giai_ba_5, 
        giai_ba_6 = :giai_ba_6, modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_ba_1", $giai_ba_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_2", $giai_ba_2, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_ba_3", $giai_ba_3, PDO::PARAM_STR );    
        $command->bindValue ( ":giai_ba_4", $giai_ba_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_5", $giai_ba_5, PDO::PARAM_STR );  
        $command->bindValue ( ":giai_ba_6", $giai_ba_6, PDO::PARAM_STR );   
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateGiaiNhi($id,$giai_nhi_1,$giai_nhi_2,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_nhi_1 = :giai_nhi_1, giai_nhi_2 = :giai_nhi_2,
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhi_1", $giai_nhi_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_nhi_2", $giai_nhi_2, PDO::PARAM_STR );     
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateGiaiNhat($id,$giai_nhat,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_nhat = :giai_nhat,
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhat", $giai_nhat, PDO::PARAM_STR );     
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }
    public function updateDacbiet($id,$giai_dacbiet,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_mienbac SET giai_dacbiet = :giai_dacbiet,
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_dacbiet", $giai_dacbiet, PDO::PARAM_STR );     
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();

        return $result;
    }


    public function insertThongke($ketqua_id,$boso,$ngay_quay,$giai,$create_user){
        $kqmienbac = new AdminKqMienbac();
        $last_id = 0;
        $timestamp = strtotime($ngay_quay);
        $day = getdate($timestamp);
        $thu = StringUtils::getDayVi($day["wday"]);

        $dau = substr($boso,0,1);
        $cuoi = substr($boso,1,1);

        $dau = intval($dau);
        $cuoi = intval($cuoi);

        $is_dacbiet = $giai=="GDB" ? 1:0;
        $is_tongchan = ($dau+$cuoi)%2 ==0 ? 1:0;
        $is_tongle = ($dau+$cuoi)%2 !=0 ? 1:0;
        $is_bochanle = ($dau%2==0 && $cuoi%2!=0)? 1 :0;
        $is_bolechan = ($dau%2!=0 && $cuoi%2==0)? 1 :0;
        $is_bochanchan = ($dau%2==0 && $cuoi%2==0)? 1 :0;
        $is_bolele = ($dau%2!=0 && $cuoi%2!=0)? 1 :0;
        $is_bokep = $dau==$cuoi ? 1:0;
        $is_bosatkep = 0;
        //echo var_dump($kqmienbac->arrSK);die;
        foreach($kqmienbac->arrSK as $value){
            if($boso == $value) {$is_bosatkep=1;break;}
        }

        $tan_so = 1;
        $tong = $dau + $cuoi;
        $tong_bo = substr($tong,1,1);

        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $last_id = 0;
        $sql = "INSERT INTO thongke_loto_mienbac(ketqua_id,province_id,boso,thu,
        is_dacbiet,is_tongchan,is_tongle,is_bochanle,is_bolechan,is_bochanchan,is_bolele,is_bokep,
        is_bosatkep,tan_so,dau_so,dit_so,tong_bo,giai,ngay_quay,create_date,create_user)
        VALUES (:ketqua_id,:province_id,:boso,:thu,:is_dacbiet,:is_tongchan,:is_tongle,
        :is_bochanle,:is_bolechan,:is_bochanchan,:is_bolele,:is_bokep,:is_bosatkep,:tan_so,:dau_so,:dit_so,
        :tong_bo,:giai,:ngay_quay,:create_date,:create_user)";

        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":ketqua_id", $ketqua_id, PDO::PARAM_INT );
        $command->bindValue ( ":province_id", 0, PDO::PARAM_INT );
        $command->bindValue ( ":boso", $boso, PDO::PARAM_STR );
        $command->bindValue ( ":thu", $thu, PDO::PARAM_INT );
        $command->bindValue ( ":is_dacbiet", $is_dacbiet, PDO::PARAM_INT );
        $command->bindValue ( ":is_tongchan", $is_tongchan, PDO::PARAM_INT );
        $command->bindValue ( ":is_tongle", $is_tongle, PDO::PARAM_INT );
        $command->bindValue ( ":is_bochanle", $is_bochanle, PDO::PARAM_INT );
        $command->bindValue ( ":is_bolechan", $is_bolechan, PDO::PARAM_INT );
        $command->bindValue ( ":is_bochanchan", $is_bochanchan, PDO::PARAM_INT );
        $command->bindValue ( ":is_bolele", $is_bolele, PDO::PARAM_INT );
        $command->bindValue ( ":is_bokep", $is_bokep, PDO::PARAM_INT );
        $command->bindValue ( ":is_bosatkep", $is_bosatkep, PDO::PARAM_INT );     
        $command->bindValue ( ":tan_so", $tan_so, PDO::PARAM_INT );
        $command->bindValue ( ":dau_so", $dau, PDO::PARAM_INT );
        $command->bindValue ( ":dit_so", $cuoi, PDO::PARAM_INT );
        $command->bindValue ( ":tong_bo", $tong_bo, PDO::PARAM_INT ); 
        $command->bindValue ( ":giai", $giai, PDO::PARAM_STR ); 
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        //echo $sql;die;
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        //echo $last_id;die;
        return $last_id;
    } 
    public function getIdByBoso($boso,$ketqua_id){
        $connect = Yii::app()->db;
        $sql = "SELECT id FROM thongke_loto_mienbac WHERE boso=".$boso." AND ketqua_id=".$ketqua_id;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
    }

    public function updateTansoThongKe($id){
        $connect = Yii::app()->db;
        $sql = "UPDATE thongke_loto_mienbac SET `tanso` = `tanso` +1 WHERE id=".$id;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
    
    public function deleteRowById($id){
        $sql = "DELETE FROM ketqua_mienbac WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
}
